#!/usr/bin/env python

import pygame, random
pygame.init()

class Bullet(pygame.sprite.Sprite):
    def __init__(self, location, direction, dest):
        pygame.sprite.Sprite.__init__(self)
        self.imageMaster = pygame.image.load("bullet.gif")
        self.imageMaster.set_colorkey((255, 0, 0))
        self.imageMaster = self.imageMaster.convert()
        self.rect = self.imageMaster.get_rect()
        
        self.location = location
        self.rect.center = self.location
        self.dir = direction
        
        #Graphical Starting Location Hack
        self.rect.centerx += 14
        self.rect.centery += 14
        
        self.trueX = float(self.rect.centerx)
        self.trueY = float(self.rect.centery)
        temp = dest[0] - location[0]
        self.xMotion = temp / 20.0
        temp = dest[1] - location[1]
        self.yMotion = temp / 20.0
        
        self.rotate()
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        
    def rotate(self):
        oldCenter = self.rect.center
        self.image = pygame.transform.rotate(self.imageMaster, self.dir)
        self.rect = self.image.get_rect()
        self.rect.center = oldCenter
        
    def update(self):
        self.trueX += self.xMotion
        self.trueY += self.yMotion
        
        self.rect.center = (self.trueX, self.trueY)
        #self.rect.inflate_ip(-4, -4)

class Label(pygame.sprite.Sprite):
    def __init__(self, font, text="", fontColor=(0, 0, 0), location=(0, 0)):
        pygame.sprite.Sprite.__init__(self)
        self.font = font
        self.text = text
        self.fontColor = fontColor
        self.location = location
        self.image = font.render(self.text, 1, self.fontColor)
        self.rect = self.image.get_rect()
        self.rect.topleft = self.location
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        
    def setText(self, text):
        self.text = text

    def update(self):
        self.image = self.font.render(self.text, 1, self.fontColor)
        self.rect = self.image.get_rect()
        self.rect.topleft = self.location
        
class Player(pygame.sprite.Sprite):
    def __init__(self, location):
        pygame.sprite.Sprite.__init__(self)
        self.imageMaster = pygame.image.load("player.gif")
        self.imageMaster.set_colorkey((255, 0, 0))
        self.imageMaster = self.imageMaster.convert()
        self.rect = self.imageMaster.get_rect()
        
        self.location = location
        self.rect.center = self.location
        self.dir = 0
        
        self.rotate()
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
    
    def rotate(self):
        oldCenter = self.rect.center
        self.image = pygame.transform.rotate(self.imageMaster, self.dir)
        self.rect = self.image.get_rect()
        self.rect.center = oldCenter
        
    def update(self):
        self.rotate()
        self.rect.inflate_ip(-20, -20)
        
    def updateDir(self, value, absolute=False):
        if absolute:
            self.dir = value
        else:
            self.dir += value
            
        valid = False
        while not valid:
            if self.dir > 360:
                self.dir -= 360
            elif self.dir < 0:
                self.dir += 360
                
            if self.dir >= 0 and self.dir <= 360:
                valid = True
                
class LineEq():
    def __init__(self):
        self.m = 0.0
        self.b = 0
        self.LeftOfCenter = True
        
    def eval(self, x):
        y = (self.m * x) + self.b
        return y
    
    def setup(self, x, y, centerx):
        self.b = y / (self.m * x)
        
        if centerx - x > 0:
            self.LeftOfCenter = True
        elif centerx - x < 0:
            self.LeftOfCenter = False
        
    def leftBy(self, val, x):
        if LeftOfCenter:
            final = self.eval(x) - val
        else:
            final = self.eval(x) + val
            
        return final
    
    def rightBy(self, val, x):
        if LeftOfCenter:
            final = self.eval(x) + val
        else:
            final = self.eval(x) - val
            
        return final
                
class CircleEnemy(pygame.sprite.Sprite):
    def __init__(self, size, location, dest, pts, delay):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((size, size))
        self.image.set_colorkey((0, 0, 0))
        self.image = self.image.convert()
        
        pygame.draw.circle(self.image, (random.randint(0, 255), 0, random.randint(0, 255)), (size/2, size/2), size/2)
        
        self.rect = self.image.get_rect()
        self.rect.center = location
        self.rect.inflate(-4, -4)
        
        self.points = pts

        self.trueX = float(self.rect.centerx)
        self.trueY = float(self.rect.centery)
        temp = dest[0] - location[0]
        self.xMotion = temp / float(delay)
        temp = dest[1] - location[1]
        self.yMotion = temp / float(delay)
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        
    def update(self, player, bullets):
        if player.rect.centerx < self.trueX:
            self.trueX += self.xMotion

            if player.rect.centerx > self.trueX:
                self.trueX = player.rect.centerx

        elif player.rect.centerx > self.trueX:
            self.trueX += self.xMotion

            if player.rect.centerx < self.trueX:
                self.trueX = player.rect.centerx

        if player.rect.centery < self.trueY:
            self.trueY += self.yMotion

            if player.rect.centery > self.trueY:
                self.trueY = player.rect.centery

        elif player.rect.centery > self.trueY:
            self.trueY += self.yMotion

            if player.rect.centery < self.trueY:
                self.trueY = player.rect.centery
        
        self.rect.center = (self.trueX, self.trueY)
                
class TriangleEnemy(pygame.sprite.Sprite):
    def __init__(self, size, location, dest, pts, delay):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((size, size))
        self.image.set_colorkey((0, 0, 0))
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        pygame.draw.polygon(self.image, (0, 200, 0), [(0, self.rect.height), (self.rect.width/2, 0), (self.rect.width, self.rect.height)])
        
        self.rect.center = location
        
        self.points = pts
        
        self.trueX = float(self.rect.centerx)
        self.trueY = float(self.rect.centery)
        
        if location[0] == dest[0]:
            location[0] -= 1
        
        self.myLine = LineEq()
        
        #If left of destination
        if dest[0]-location[0] > 0:
            dy = dest[1] - location[1]
            dx = dest[0] - location[0]
            self.myLine.m = float(dy) / dx
        #If right of destination
        else:
            dy = location[1] - dest[1]
            dx = location[0] - dest[0]
            self.myLine.m = float(dy) / dx
            
        self.myLine.setup(self.rect.centerx, self.rect.centery, dest[0])
        
        altDx = dest[0] - location[0]
        self.xMotion = altDx / float(delay)
        
    def draw(self, screen):
        screen.blit(self.image, self.rect)
        
    def update(self, player, bullets):
        self.trueY = self.myLine.eval(self.trueX)
        self.rect.center = (self.trueX, self.trueY)
        
        self.trueX += self.xMotion